package com.xydtech.ecard.custgroup.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.text.Convert;
import com.xydtech.ecard.custgroup.domain.EcardCustomerGroupMerchant;
import com.xydtech.ecard.custgroup.mapper.EcardCustomerGroupMerchantMapper;
import com.xydtech.ecard.custgroup.service.IEcardCustomerGroupMerchantService;
import com.xydtech.ecard.merchant.domain.EcardAgricultureInfo;
import com.xydtech.ecard.merchant.domain.EcardEnterpriseInfo;
import com.xydtech.ecard.merchant.domain.EcardMerchantInfo;
import com.xydtech.ecard.merchant.domain.EcardPersonInfo;
import com.xydtech.ecard.merchant.service.IEcardAgricultureInfoService;
import com.xydtech.ecard.merchant.service.IEcardEnterpriseInfoService;
import com.xydtech.ecard.merchant.service.IEcardMerchantInfoService;
import com.xydtech.ecard.merchant.service.IEcardPersonInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片行业客户信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-12-04 上午8:43
 */
@Service
public class EcardCustomerGroupMerchantServiceImpl implements IEcardCustomerGroupMerchantService
{
    @Autowired
    private EcardCustomerGroupMerchantMapper ecardCustomerGroupMerchantMapper;
    @Autowired
    private IEcardPersonInfoService ecardPersonInfoService;
    @Autowired
    private IEcardMerchantInfoService ecardMerchantInfoService;
    @Autowired
    private IEcardEnterpriseInfoService ecardEnterpriseInfoService;
    @Autowired
    private IEcardAgricultureInfoService ecardAgricultureInfoService;

    /**
     * 查询电子名片行业客户信息
     *
     * @param id 电子名片行业客户信息主键
     * @return 电子名片行业客户信息
     */
    @Override
    @DataScope(userAlias = "u", deptAlias = "d")
    public EcardCustomerGroupMerchant selectEcardCustomerGroupMerchantById(Long id)
    {
        return ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantById(id);
    }

    /**
     * 查询电子名片行业客户信息列表
     *
     * @param ecardCustomerGroupMerchant 电子名片行业客户信息
     * @return 电子名片行业客户信息
     */
    @Override
    @DataScope(userAlias = "u", deptAlias = "d")
    public List<EcardCustomerGroupMerchant> selectEcardCustomerGroupMerchantList(EcardCustomerGroupMerchant ecardCustomerGroupMerchant)
    {
        return ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantList(ecardCustomerGroupMerchant);
    }

    /**
     * 新增电子名片行业客户信息
     *
     * @param ecardCustomerGroupMerchant 电子名片行业客户信息
     * @return 结果
     */
    @Override
    public int insertEcardCustomerGroupMerchant(EcardCustomerGroupMerchant ecardCustomerGroupMerchant)
    {
        return ecardCustomerGroupMerchantMapper.insertEcardCustomerGroupMerchant(ecardCustomerGroupMerchant);
    }

    /**
     * 修改电子名片行业客户信息
     *
     * @param ecardCustomerGroupMerchant 电子名片行业客户信息
     * @return 结果
     */
    @Override
    public int updateEcardCustomerGroupMerchant(EcardCustomerGroupMerchant ecardCustomerGroupMerchant)
    {
        return ecardCustomerGroupMerchantMapper.updateEcardCustomerGroupMerchant(ecardCustomerGroupMerchant);
    }

    /**
     * 批量删除电子名片行业客户信息
     *
     * @param ids 需要删除的电子名片行业客户信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardCustomerGroupMerchantByIds(String ids)
    {
        return ecardCustomerGroupMerchantMapper.deleteEcardCustomerGroupMerchantByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片行业客户信息信息
     *
     * @param id 电子名片行业客户信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardCustomerGroupMerchantById(Long id)
    {
        return ecardCustomerGroupMerchantMapper.deleteEcardCustomerGroupMerchantById(id);
    }

    /**
     * 批量移交行业客户信息
     * @param ecardCustomerGroupMerchantList
     * @param registId
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int allocationEcardGroupMerchantInfo(List<EcardCustomerGroupMerchant> ecardCustomerGroupMerchantList, Long registId, Long userId) {
        //根据商户类型和商户id调用对应移交方法，方法里面同步移交行业客户信息
        List<EcardPersonInfo> ecardPersonInfoList = new ArrayList<>();
        List<EcardMerchantInfo> ecardMerchantInfoList = new ArrayList<>();
        List<EcardEnterpriseInfo> ecardEnterpriseInfoList = new ArrayList<>();
        List<EcardAgricultureInfo> ecardAgricultureInfoList = new ArrayList<>();
        for (EcardCustomerGroupMerchant customerGroupMerchant : ecardCustomerGroupMerchantList) {
            //根据主键查询行业客户信息
            EcardCustomerGroupMerchant groupMerchant = ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantById(customerGroupMerchant.getId());
            if ("1".equals(groupMerchant.getMerchantType())) {//个人
                //修改创建人id和创建人机构id
                EcardPersonInfo ecardPersonInfo = new EcardPersonInfo();
                ecardPersonInfo.setId(groupMerchant.getMerchantId());
                ecardPersonInfo.setManagementPersonnelId(customerGroupMerchant.getManagementPersonnelId());
                ecardPersonInfo.setDeptId(customerGroupMerchant.getDeptId());
                ecardPersonInfoList.add(ecardPersonInfo);
            }
            if ("2".equals(groupMerchant.getMerchantType())) {//商户
                //修改创建人id和创建人机构id
                EcardMerchantInfo ecardMerchantInfo = new EcardMerchantInfo();
                ecardMerchantInfo.setId(groupMerchant.getMerchantId());
                ecardMerchantInfo.setCreatorId(customerGroupMerchant.getManagementPersonnelId());
                ecardMerchantInfo.setDeptId(customerGroupMerchant.getDeptId());
                ecardMerchantInfoList.add(ecardMerchantInfo);
            }
            if ("3".equals(groupMerchant.getMerchantType())) {//企业
                //修改创建人id和创建人机构id
                EcardEnterpriseInfo ecardEnterpriseInfo = new EcardEnterpriseInfo();
                ecardEnterpriseInfo.setId(groupMerchant.getMerchantId());
                ecardEnterpriseInfo.setManagementPersonnelId(customerGroupMerchant.getManagementPersonnelId());
                ecardEnterpriseInfo.setDeptId(customerGroupMerchant.getDeptId());
                ecardEnterpriseInfoList.add(ecardEnterpriseInfo);
            }
            if ("4".equals(groupMerchant.getMerchantType())) {//新型农业主体
                //修改创建人id和创建人机构id
                EcardAgricultureInfo ecardAgricultureInfo = new EcardAgricultureInfo();
                ecardAgricultureInfo.setId(groupMerchant.getMerchantId());
                ecardAgricultureInfo.setManagementPersonnelId(customerGroupMerchant.getManagementPersonnelId());
                ecardAgricultureInfo.setDeptId(customerGroupMerchant.getDeptId());
                ecardAgricultureInfoList.add(ecardAgricultureInfo);
            }
        }
        int a = 0;
        if (ecardPersonInfoList.size() > 0) {
            //批量移交个人信息，方法里面同步移交行业客户信息
            a = ecardPersonInfoService.allocationEcardPersonInfo(ecardPersonInfoList, registId, userId);
        }
        if (ecardMerchantInfoList.size() > 0) {
            //批量移交商户信息，方法里面同步移交行业客户信息
            a = ecardMerchantInfoService.allocationEcardMerchantInfo(ecardMerchantInfoList, registId, userId);
        }
        if (ecardEnterpriseInfoList.size() > 0) {
            //批量移交企业信息，方法里面同步移交行业客户信息
            a = ecardEnterpriseInfoService.allocationEcardEnterpriseInfo(ecardEnterpriseInfoList, registId, userId);
        }
        if (ecardAgricultureInfoList.size() > 0) {
            //批量移交新型农业主体信息，方法里面同步移交行业客户信息
            a = ecardAgricultureInfoService.allocationEcardAgricultureInfo(ecardAgricultureInfoList, registId, userId);
        }
        return a;
    }

    /**
     * 批量释放行业客户信息
     * @param ecardCustomerGroupMerchantList
     * @return
     */
    @Override
    public int batchReleaseEcardCustomerGroupMerchant(List<EcardCustomerGroupMerchant> ecardCustomerGroupMerchantList) {
        //根据商户类型和商户id调用对应释放方法，方法里面同步释放行业客户信息
        List<EcardPersonInfo> ecardPersonInfoList = new ArrayList<>();
        List<EcardMerchantInfo> ecardMerchantInfoList = new ArrayList<>();
        List<EcardEnterpriseInfo> ecardEnterpriseInfoList = new ArrayList<>();
        List<EcardAgricultureInfo> ecardAgricultureInfoList = new ArrayList<>();
        for (EcardCustomerGroupMerchant customerGroupMerchant : ecardCustomerGroupMerchantList) {
           if ("1".equals(customerGroupMerchant.getMerchantType())) {//个人
                //修改创建人id置为null
                EcardPersonInfo ecardPersonInfo = new EcardPersonInfo();
                ecardPersonInfo.setId(customerGroupMerchant.getMerchantId());
                ecardPersonInfo.setManagementPersonnelId(null);
                ecardPersonInfoList.add(ecardPersonInfo);
            }
            if ("2".equals(customerGroupMerchant.getMerchantType())) {//商户
                //修改创建人id置为null
                EcardMerchantInfo ecardMerchantInfo = new EcardMerchantInfo();
                ecardMerchantInfo.setId(customerGroupMerchant.getMerchantId());
                ecardMerchantInfo.setCreatorId(null);
                ecardMerchantInfoList.add(ecardMerchantInfo);
            }
            if ("3".equals(customerGroupMerchant.getMerchantType())) {//企业
                //修改创建人id置为null
                EcardEnterpriseInfo ecardEnterpriseInfo = new EcardEnterpriseInfo();
                ecardEnterpriseInfo.setId(customerGroupMerchant.getMerchantId());
                ecardEnterpriseInfo.setManagementPersonnelId(null);
                ecardEnterpriseInfoList.add(ecardEnterpriseInfo);
            }
            if ("4".equals(customerGroupMerchant.getMerchantType())) {//新型农业主体
                //修改创建人id置为null
                EcardAgricultureInfo ecardAgricultureInfo = new EcardAgricultureInfo();
                ecardAgricultureInfo.setId(customerGroupMerchant.getMerchantId());
                ecardAgricultureInfo.setManagementPersonnelId(null);
                ecardAgricultureInfoList.add(ecardAgricultureInfo);
            }
        }
        int a = 0;
        if (ecardPersonInfoList.size() > 0) {
            //批量释放个人信息，方法里面同步释放行业客户信息
            a = ecardPersonInfoService.batchReleaseEcardPersonInfo(ecardPersonInfoList);
        }
        if (ecardMerchantInfoList.size() > 0) {
            //批量释放商户信息，方法里面同步释放行业客户信息
            a = ecardMerchantInfoService.batchReleaseEcardMerchantInfo(ecardMerchantInfoList);
        }
        if (ecardEnterpriseInfoList.size() > 0) {
            //批量释放企业信息，方法里面同步释放行业客户信息
            a = ecardEnterpriseInfoService.batchReleaseEcardEnterpriseInfo(ecardEnterpriseInfoList);
        }
        if (ecardAgricultureInfoList.size() > 0) {
            //批量释放新型农业主体信息，方法里面同步释放行业客户信息
            a = ecardAgricultureInfoService.batchReleaseEcardAgricultureInfo(ecardAgricultureInfoList);
        }
        return a;
    }

    /**
     * 批量启用/停用行业客户
     * @param ecardCustomerGroupMerchantList
     * @return
     */
    @Override
    public int batchSwitchOverEcardCustomerGroupMerchantInfo(List<EcardCustomerGroupMerchant> ecardCustomerGroupMerchantList) {
        //根据商户类型和商户id调用对应启用/停用方法，方法里面同步启用/停用行业客户信息
        List<EcardPersonInfo> ecardPersonInfoList = new ArrayList<>();
        List<EcardMerchantInfo> ecardMerchantInfoList = new ArrayList<>();
        List<EcardEnterpriseInfo> ecardEnterpriseInfoList = new ArrayList<>();
        List<EcardAgricultureInfo> ecardAgricultureInfoList = new ArrayList<>();
        for (EcardCustomerGroupMerchant customerGroupMerchant : ecardCustomerGroupMerchantList) {
            if ("1".equals(customerGroupMerchant.getMerchantType())) {//个人
                //修改个人状态
                EcardPersonInfo ecardPersonInfo = new EcardPersonInfo();
                ecardPersonInfo.setId(customerGroupMerchant.getMerchantId());
                ecardPersonInfo.setState(customerGroupMerchant.getState());
                ecardPersonInfoList.add(ecardPersonInfo);
            }
            if ("2".equals(customerGroupMerchant.getMerchantType())) {//商户
                //修改商户状态
                EcardMerchantInfo ecardMerchantInfo = new EcardMerchantInfo();
                ecardMerchantInfo.setId(customerGroupMerchant.getMerchantId());
                ecardMerchantInfo.setState(customerGroupMerchant.getState());
                ecardMerchantInfoList.add(ecardMerchantInfo);
            }
            if ("3".equals(customerGroupMerchant.getMerchantType())) {//企业
                //修改企业状态
                EcardEnterpriseInfo ecardEnterpriseInfo = new EcardEnterpriseInfo();
                ecardEnterpriseInfo.setId(customerGroupMerchant.getMerchantId());
                ecardEnterpriseInfo.setState(customerGroupMerchant.getState());
                ecardEnterpriseInfoList.add(ecardEnterpriseInfo);
            }
            if ("4".equals(customerGroupMerchant.getMerchantType())) {//新型农业主体
                //修改新型农业主体状态
                EcardAgricultureInfo ecardAgricultureInfo = new EcardAgricultureInfo();
                ecardAgricultureInfo.setId(customerGroupMerchant.getMerchantId());
                ecardAgricultureInfo.setState(customerGroupMerchant.getState());
                ecardAgricultureInfoList.add(ecardAgricultureInfo);
            }
        }
        int a = 0;
        if (ecardPersonInfoList.size() > 0) {
            //批量启用/停用个人信息，方法里面同步启用/停用行业客户信息
            a = ecardPersonInfoService.batchSwitchOverEcardPersonInfo(ecardPersonInfoList);
        }
        if (ecardMerchantInfoList.size() > 0) {
            //批量启用/停用商户信息，方法里面同步启用/停用行业客户信息
            a = ecardMerchantInfoService.batchSwitchOverEcardMerchantInfo(ecardMerchantInfoList);
        }
        if (ecardEnterpriseInfoList.size() > 0) {
            //批量启用/停用企业信息，方法里面同步启用/停用行业客户信息
            a = ecardEnterpriseInfoService.batchSwitchOverEcardMerchantInfo(ecardEnterpriseInfoList);
        }
        if (ecardAgricultureInfoList.size() > 0) {
            //批量启用/停用新型农业主体信息，方法里面同步启用/停用行业客户信息
            a = ecardAgricultureInfoService.batchSwitchOverEcardAgricultureInfo(ecardAgricultureInfoList);
        }
        return a;
    }
}
